Completed
Push — master ( a14198...1b9c43 )
by Andres
27s
created

angular.controller(ꞌct_reactionsꞌ)   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
c 1
b 0
f 0
nc 5
dl 0
loc 15
rs 8.8571
nop 2
1
/**
2
 reactions
3
 Component that handles reactions and molecules.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('reactions', {
10
  templateUrl: 'views/reactions.html',
11
  controller:  'ct_reactions',
12
  controllerAs: 'ct'
13
});
14
15
angular.module('game').controller('ct_reactions', ['state', 'data', 'visibility', 'util', 'format', 'reaction',
16
function (state, data, visibility, util, format, reactionService) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
  ct.util = util;
21
  ct.format = format;
22
23
  function update(player) {
24
    for (let reaction of player.reactions) {
25
      if (!reaction.active) {
26
        continue;
27
      }
28
      reactionService.react(numberToReact(player, reaction.reaction), reaction.reaction, player);
29
    }
30
  }
31
32
  function numberToReact(player, reaction) {
33
    let power = ct.reactionPower(player);
34
    let number = power;
35
    for(let resource in reaction.reactants){
36
      number = Math.min(number, player.resources[resource].number);
37
    }
38
    return number;
39
  }
40
41
  /* Calculates the redox power based on the redox upgrades */
42
  ct.reactionPower = function(player) {
43
    let level = player.global_upgrades.reaction_bandwidth;
44
    let upgrade = data.global_upgrades.reaction_bandwidth;
45
    let basePower = upgrade.power;
46
    let polynomial = upgrade.power_poly;
47
    return basePower * Math.floor(Math.pow(level, polynomial));
48
  };
49
50
  /* Calculates the number of redox slots based on the redox upgrades */
51
  ct.reactionSlots = function (player) {
52
    let level = player.global_upgrades.reaction_slots;
53
    let upgrade = data.global_upgrades.reaction_slots;
54
    let basePower = upgrade.power;
55
    let multiplier = upgrade.power_mult;
56
    return basePower * Math.floor(multiplier * level);
57
  };
58
59
  ct.reactionSize = function (player) {
60
    return player.reactions.length;
61
  };
62
63
  /* Adds a new reaction to the player list */
64
  ct.addReaction = function (player, key) {
65
    if(ct.reactionSize(player) >= ct.reactionSlots(player)){
66
      return;
67
    }
68
    let reaction = data.reactions[key];
69
    player.reactions.push({
70
      active: false,
71
      reaction: angular.copy(reaction)
72
    });
73
  };
74
75
  ct.removeReaction = function (player, item) {
76
    for(let i = 0; i < player.reactions.length; i++){
77
      if(player.reactions[i] === item){
78
        player.reactions.splice(i, 1);
79
      }
80
    }
81
  };
82
83
  ct.visibleReactions = function(currentElement) {
84
    return visibility.visible(state.player.reactions, isReactionVisible, currentElement);
85
  };
86
87
  function isReactionVisible(entry, currentElement) {
88
    let reaction = entry.reaction;
89
    for(let resource in reaction.reactant){
90
      let elements = data.resources[resource].elements;
91
      if(Object.keys(elements).length === 0 && currentElement === ''){
92
        return true;
93
      }
94
      for(let element in elements){
95
        if(element === currentElement){
96
          return true;
97
        }
98
      }
99
    }
100
    return false;
101
  }
102
103
  ct.availableReactions = function(currentElement) {
104
    return visibility.visible(data.reactions, isReactionAvailable, currentElement);
105
  };
106
107
  function isReactionAvailable(entry, currentElement) {
108
    let available = true;
109
    let reaction = data.reactions[entry];
110
    for(let resource in reaction.reactant){
111
      available = available && state.player.resources[resource].unlocked;
112
    }
113
    // Workaround to reuse the visibility function. It expects an object with the
114
    // reaction inside
115
    let reactionObject = {reaction:reaction};
116
    return available && isReactionVisible(reactionObject, currentElement);
117
  }
118
119
  state.registerUpdate('reactions', update);
120
}]);
121